package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import model.InvIndex;

/**
 * The Read class works with the selected directory, building the inverted index
 * of the files within it.
 * 
 * @author Diogo Kiss and Valerio Lopes
 */
public class Read
{

	/**
	 * The ler method scans the files in the given directory and calls the
	 * varrerArquivo method to read each one of them, building the inverted
	 * index.
	 * 
	 * @param File
	 *            dir - The directory to be read.
	 * @param InvIndex
	 *            index - The empty object InvIndex, in which the inverted index
	 *            will be constructed.
	 * @return It doesn't return nothing explicitly (it's a void method), but at
	 *         the end of the method the inverted index is constructed.
	 */
	public void scan(File dir, InvIndex index, boolean stem)
	{

		for (File file : dir.listFiles())
		{

			if (file.isFile())
			{
				scanFile(file,index,stem);
			}

		}

		for (File f : dir.listFiles())
		{
			if (f.isDirectory())
				scan(f,index,stem);
		}

	}

	/**
	 * The varrerArquivo method scans the given file and calls the method
	 * addWord to add the word in the inverted index, for each word in the file.
	 * 
	 * @param File
	 *            arq - The file to be read.
	 * @param InvIndex
	 *            indice - The inverted index under construction.
	 * @return It doesn't return nothing explicitly (it's a void method). It is
	 *         part of the process of constructing the inverted index.
	 * @exception IOException
	 *                if some problem occurs when handling the file.
	 */
	private void scanFile(File arq, InvIndex indice, boolean stem)
	{
		
		Stemmer stemmer= new Stemmer();
		BufferedReader bf;
		try
		{

			bf = new BufferedReader(new FileReader(arq));

			String s;

			// Iterating of each file
			while ((s = bf.readLine()) != null)
			{

				String[] linha = s.split("[,|.|\\s|\\[|\\]|{|}|*|!|&|%|$|#|+|=|_|/|(|)|'|\"|:|;|-]");

				for (String s1 : linha)
				{

					if (!"".equals(s1))
					{
						String palavra = s1.trim().toLowerCase();
						
						if(stem)
							palavra = stemmer.getStemmed(palavra);

						indice.addWord(arq.getName(),palavra);
					}

				}
			}
			
			bf.close();

		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public static Vector<String> getStopWords()
	{
		BufferedReader bf;
		Vector<String> stopwords = new Vector<String>();

		try
		{

			bf = new BufferedReader(new FileReader("res/stopwords.txt"));

			String s;

			// Iterating of each file
			while ((s = bf.readLine()) != null)
			{
				stopwords.add(s);
			}

			bf.close();

		} catch (IOException e)
		{
			e.printStackTrace();
		}

		return stopwords;
	}

}
